${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}

<#assign classbody>
<#assign declarationName = pojo.importType(pojo.getDeclarationName())>/**
 * Home object for domain model class ${declarationName}.
 * @see ${pojo.getQualifiedDeclarationName()}
 * @author Hibernate Tools
 */
<#if ejb3>
@${pojo.importType("javax.ejb.Stateless")}
</#if>

public class ${declarationName}Home extends ${pojo.importType("it.pecuswap.backend.entities.base.BaseHome")}{

    private static final ${pojo.importType("org.apache.log4j.Logger")} logger = Logger.getLogger(${pojo.getDeclarationName()}Home.class);

<#if ejb3>
    
    public void persist(${declarationName} transientInstance) throws EntityHomeException{
        logger.debug("persisting ${declarationName} instance");
        try {
            session.persist(transientInstance);
            logger.debug("persist successful");
        }
        catch (RuntimeException e) {
            logger.error("persist failed", e);
            throw new EntityHomeException();
        }
    }
    
    public void remove(${declarationName} persistentInstance) throws EntityHomeException{
        logger.debug("removing ${declarationName} instance");
        try {
            session.remove(persistentInstance);
            logger.debug("remove successful");
        }
        catch (RuntimeException e) {
            logger.error("remove failed", e);
            throw new EntityHomeException();
        }
    }
    
    public ${declarationName} merge(${declarationName} detachedInstance) throws EntityHomeException{
        logger.debug("merging ${declarationName} instance");
        try {
            ${declarationName} result = session.merge(detachedInstance);
            logger.debug("merge successful");
            return result;
        }
        catch (RuntimeException e) {
            logger.error("merge failed", e);
            throw new EntityHomeException();
        }
    }
    
<#if clazz.identifierProperty?has_content>    
    public ${declarationName} findById( ${pojo.getJavaTypeName(clazz.identifierProperty, jdk5)} id) {
        logger.debug("getting ${declarationName} instance with id: " + id);
        try {
            ${declarationName} instance = session.find(${pojo.getDeclarationName()}.class, id);
            logger.debug("get successful");
            return instance;
        }
        catch (RuntimeException e) {
            logger.error("get failed", e);
            throw new EntityHomeException();
        }
    }
</#if>
<#else>    
    
    public void persist(${declarationName} transientInstance) throws EntityHomeException{
        logger.debug("persisting ${declarationName} instance");
        try {
            session.persist(transientInstance);
            logger.debug("persist successful");
        }
        catch (RuntimeException e) {
            logger.error("persist failed", e);
            throw new ${pojo.importType("it.pecuswap.backend.exceptions.EntityHomeException")}();
        }
    }
    
    public void save(${declarationName} instance) throws EntityHomeException{
        logger.debug("saving ${declarationName} instance");
        try {
            session.save(instance);
            logger.debug("save successful");
        }
        catch (RuntimeException e) {
            logger.error("save failed", e);
            throw new EntityHomeException();
        }
    }
    
    public void update(${declarationName} instance) throws EntityHomeException{
        logger.debug("updating clean ${declarationName} instance");
        try {
            session.update(instance);
            logger.debug("update successful");
        }
        catch (RuntimeException e) {
            logger.error("update failed", e);
            throw new EntityHomeException();
        }
    }
    
    public void delete(${declarationName} persistentInstance) throws EntityHomeException{
        logger.debug("deleting ${declarationName} instance");
        try {
            session.delete(persistentInstance);
            logger.debug("delete successful");
        }
        catch (RuntimeException e) {
            logger.error("delete failed", e);
            throw new EntityHomeException();
        }
    }
    
    public ${declarationName} merge(${declarationName} detachedInstance) throws EntityHomeException{
        logger.debug("merging ${declarationName} instance");
        try {
            ${declarationName} result = (${declarationName}) session.merge(detachedInstance);
            logger.debug("merge successful");
            return result;
        }
        catch (RuntimeException e) {
            logger.error("merge failed", e);
            throw new EntityHomeException();
        }
    }
    
<#if clazz.identifierProperty?has_content>
    public ${declarationName} findById( ${c2j.getJavaTypeName(clazz.identifierProperty, jdk5)} id) throws EntityHomeException{
        logger.debug("getting ${declarationName} instance with id: " + id);
        try {
            ${declarationName} instance = (${declarationName}) session.get(${pojo.getDeclarationName()}.class, id);
            if (instance==null) {
                logger.debug("get successful, no instance found");
            }
            else {
                logger.debug("get successful, instance found");
            }
            return instance;
        }
        catch (RuntimeException e) {
            logger.error("get failed", e);
            throw new EntityHomeException();
        }
    }
</#if>
    
<#if clazz.hasNaturalId()>
    public ${declarationName} findByNaturalId(${c2j.asNaturalIdParameterList(clazz)}) {
        logger.debug("getting ${declarationName} instance by natural id");
        try {
            ${declarationName} instance = (${declarationName}) session.createCriteria("${clazz.entityName}")
<#if jdk5>
                    .add( ${pojo.staticImport("org.hibernate.criterion.Restrictions", "naturalId")}()
<#else>
                   .add( ${pojo.importType("org.hibernate.criterion.Restrictions")}.naturalId()
</#if>                    
<#foreach property in pojo.getAllPropertiesIterator()>
<#if property.isNaturalIdentifier()>
                            .set("${property.name}", ${property.name})
</#if>
</#foreach>
                        )
                    .uniqueResult();
            if (instance==null) {
                logger.debug("get successful, no instance found");
            }
            else {
                logger.debug("get successful, instance found");
            }
            return instance;
        }
        catch (RuntimeException e) {
            logger.error("query failed", e);
            throw new EntityHomeException();
        }
    }
</#if> 

@SuppressWarnings("unchecked")
public ${pojo.importType("java.util.List")}<${declarationName}> findByExample(${declarationName} instance) throws EntityHomeException{
	logger.debug("finding ${declarationName} instance by example");
    try {
		${pojo.importType("org.hibernate.Criteria")} criteria = session.createCriteria(${pojo.getDeclarationName()}.class);
		${pojo.importType("org.hibernate.criterion.Example")} example = Example.create(instance);
		criteria.add(example);
		List<${declarationName}> results = criteria.list();
		logger.debug("find by example successful, esult size: " + results.size());
        return results;
    }
    catch (RuntimeException e) {
        logger.error("find by example failed", e);
        throw new EntityHomeException();
    }
} 
   
<#foreach queryName in cfg.namedQueries.keySet()>
<#if queryName.startsWith(clazz.entityName + ".")>
<#assign methname = c2j.unqualify(queryName)>
<#assign params = cfg.namedQueries.get(queryName).parameterTypes><#assign argList = c2j.asFinderArgumentList(params, pojo)>
<#if jdk5 && methname.startsWith("find")>
    public ${pojo.importType("java.util.List")}<${declarationName}> ${methname}(${argList}) {
<#elseif methname.startsWith("count")>
    public int ${methname}(${argList}) {
<#else>
    public ${pojo.importType("java.util.List")} ${methname}(${argList}) {
</#if>
        ${pojo.importType("org.hibernate.Query")} query = session.getNamedQuery("${queryName}");
<#foreach param in params.keySet()>
<#if param.equals("maxResults")>
		query.setMaxResults(maxResults);
<#elseif param.equals("firstResult")>
        query.setFirstResult(firstResult);
<#else>
        query.setParameter("${param}", ${param});
</#if>
</#foreach>
<#if jdk5 && methname.startsWith("find")>
        return (List<${declarationName}>) query.list();
<#elseif methname.startsWith("count")>
        return ( (Integer) query.uniqueResult() ).intValue();
<#else>
        return query.list();
</#if>
    }
</#if>
</#foreach></#if>
}
</#assign>

${pojo.generateImports()}
${classbody}
